/*
 * Copyright 2021 ADTIMING TECHNOLOGY COMPANY LIMITED
 * Licensed under the GNU Lesser General Public License Version 3
 */

package com.openmediation.sdk.utils;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import com.openmediation.sdk.core.InsManager;
import com.openmediation.sdk.utils.model.BaseInstance;
import com.openmediation.sdk.utils.model.Placement;

public class FirebaseUtil {
    private static final String CLAZZ = "com.google.firebase.analytics.FirebaseAnalytics";
    public static final String EVENT_IMPRESSION_REVENUE = "Ad_Impression_Revenue";
    public static final String EVENT_TOP_10 = "AdLTV_OneDay_Top10Percent";
    public static final String EVENT_TOP_20 = "AdLTV_OneDay_Top20Percent";
    public static final String EVENT_TOP_30 = "AdLTV_OneDay_Top30Percent";
    public static final String EVENT_TOP_40 = "AdLTV_OneDay_Top40Percent";
    public static final String EVENT_TOP_50 = "AdLTV_OneDay_Top50Percent";

    public static void reportUserAdRevenue(BaseInstance instance) {
        DeveloperLog.LogD("FirebaseAnalytics Upload Ad_Impression_Revenue Event");
        try {
            Class clazz = Class.forName(CLAZZ);
        } catch (Throwable e) {
            AdLog.getSingleton().LogE("FirebaseAnalytics SDK Not Found Error: " + e.getMessage());
            return;
        }
        Application context = AdtUtil.getInstance().getApplicationContext();
        if (context == null) {
            return;
        }
        com.google.firebase.analytics.FirebaseAnalytics firebaseAnalytics = com.google.firebase.analytics.FirebaseAnalytics.getInstance(context);
        Bundle bundle = new Bundle();
        double revenue = instance.getShowRevenue(6);
        bundle.putDouble(com.google.firebase.analytics.FirebaseAnalytics.Param.VALUE, revenue);
        // The revenue generated by the user in USD, expressed with 6 decimal precision.
        bundle.putString(com.google.firebase.analytics.FirebaseAnalytics.Param.CURRENCY, "USD");
        // Display name of the network which showed the ad (e.g. "AdMob")
        String networkName = InsManager.getNetworkName(instance);
        bundle.putString("adNetwork", networkName);
        Placement placement = PlacementUtils.getPlacement(instance.getPlacementId());
        if (placement != null) {
            String adFormat = PlacementUtils.getAdType(placement.getT());
            bundle.putString("adFormat", adFormat);
        }
        firebaseAnalytics.logEvent(EVENT_IMPRESSION_REVENUE, bundle);
    }

    public static void reportTopXEvent(int level) {
        String uarEventId = getUarEventId(level);
        if (TextUtils.isEmpty(uarEventId)) {
            return;
        }

        DeveloperLog.LogD("FirebaseAnalytics Upload TopX Event : " + uarEventId);

        try {
            Class clazz = Class.forName(CLAZZ);
        } catch (Throwable e) {
            AdLog.getSingleton().LogE("FirebaseAnalytics SDK Not Found Error: " + e.getMessage());
            return;
        }
        Application context = AdtUtil.getInstance().getApplicationContext();
        if (context == null) {
            return;
        }
        com.google.firebase.analytics.FirebaseAnalytics firebaseAnalytics = com.google.firebase.analytics.FirebaseAnalytics.getInstance(context);
        Bundle bundle = new Bundle();
        firebaseAnalytics.logEvent(uarEventId, bundle);
    }

    private static String getUarEventId(int level) {
        switch (level) {
            case 1:
                return EVENT_TOP_50;
            case 2:
                return EVENT_TOP_40;
            case 3:
                return EVENT_TOP_30;
            case 4:
                return EVENT_TOP_20;
            case 5:
                return EVENT_TOP_10;
            default:
                return "";
        }
    }
}
